<template>
    <div class="order-edit-bar">
        <div class="flex"></div>
        <slot></slot>
        <div class="order-edit-button" v-if="order.order_state===1" @click="onCancel">取消订单</div>
        <div class="order-edit-button" v-if="order.order_state===1" @click="onPay">付款</div>
        <div class="order-edit-button" v-if="order.order_state===2" @click="onNotice">提醒卖家发货</div>
        <div class="order-edit-button" v-if="order.order_state===3" @click="onConfirm">确认收货</div>
        <div class="order-edit-button" v-if="order.order_state===4&&order.buyer_rate===0" @click="onRate">评价</div>
        <div class="order-edit-button" v-if="order.order_state===5" @click="onDelete">删除订单</div>
    </div>
</template>

<script>
    export default {
        name: "OrderEditBar",
        data() {
            return {}
        },
        props: {
            order: Object
        },
        mounted() {
        },
        methods: {
            onCancel(val) {
                this.$emit('cancel', this.order);
            },
            onPay() {
                this.$get('/webapi/wechatpay/getconfig?order_id=' + this.order.order_id).then(response => {
                    //console.log(response.data);
                    var config = response.data.config;
                    config.timestamp = config.timeStamp;
                    config.success = (res) => {
                        this.$toast.success('付款成功');
                        this.$emit('pay', this.order);
                    };
                    wx.chooseWXPay(config);
                }).catch(reason => {
                    if (reason.data){
                        this.$toast.fail(reason.data.errmsg);
                    }
                });
            },
            onNotice() {
                this.$post('/webapi/bought/notice', {
                    order_id: this.order.order_id
                }).then(response => {
                    this.$toast.success('已提醒卖家发货');
                    this.$emit('notice', this.order);
                });
            },
            onConfirm() {
                this.$dialog.confirm({
                    title: '确认收货',
                    message: '在确认收货之前请检查已收到货物是否完好'
                }).then(() => {
                    // on confirm
                    this.$post('/webapi/bought/confirm', {
                        order_id: this.order.order_id
                    }).then(response => {
                        this.$toast.success('确认收货成功');
                        this.$emit('confirm', this.order);
                    }).catch(reason => {
                        if (reason.data) {
                            this.$toast.fail(reason.data.errmsg);
                        }
                    });
                }).catch(() => {
                    // on cancel
                });
            },
            onRefund() {
                this.$dialog.confirm({
                    title: null,
                    message: '你确认要申请退款吗?'
                }).then(() => {
                    // on confirm
                    this.$axios.post('/webapi/bought/applyrefund', {
                        order_id: this.order.order_id
                    }).then(response => {
                        this.$toast.success('退款成功');
                        this.$emit('refund', this.order);
                    });
                }).catch(() => {
                    // on cancel
                });
            },
            onDelete() {
                this.$dialog.confirm({
                    title: '删除确认',
                    message: '确认要删除此订单吗?'
                }).then(() => {
                    this.$axios.post('/webapi/bought/delete', {
                        order_id: this.order.order_id
                    }).then(response => {
                        this.$emit('delete', this.order);
                    });
                }).catch(() => {
                    // on cancel
                });

            },
            onRate() {
                window.location.href = '/h5/order/rate?order_id='+this.order.order_id
            }
        }
    }
</script>

<style scoped>

</style>
